package com.aramco.fion.model.presentation
{
	import com.aramco.fion.event.LoadQueryEvent;
	import com.aramco.fion.util.SelectionUtil;
	import com.aramco.fion.view.SelectionReport;
	import com.aramco.fion.view.selection.Popup.BASE_POPUP_SELECTION;
	import com.aramco.fion.view.selection.Popup.F4_CC_SELEC_POPUP;
	import com.aramco.fion.vo.F4Value;
	import com.aramco.fion.vo.SelectionScreenVo;
	import com.aramco.fion.vo.selection.SelectionVo;
	import com.aramco.fion.vo.selection.SingleMultipleSelectionVo;
	import com.codecatalyst.util.ArrayUtil;
	
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DataGrid;
	import mx.events.FlexEvent;
	import mx.events.ItemClickEvent;
	import mx.managers.PopUpManager;
	import mx.utils.ObjectUtil;
	
	[Bindable]
	public class F4_CC_SELEC_POPUP_PresentataionModel
	{
		public var view:F4_CC_SELEC_POPUP;
		public function F4_CC_SELEC_POPUP_PresentataionModel()
		{
		}
		
		//private var _selectionMeta:ArrayCollection;
		
		[Dispatcher]
		public var dispatcher:IEventDispatcher;
		
		[Bindable]
		public var selectionMeta:SelectionScreenVo;
		
		[Bindable]
		private var _f4Values:XMLList;
		

		
		//-------- UI Variables -------------------		
		[Bindable]
		var includeExcluded_ToggleButtonBar_Selected = true;
		
		[Bindable]
		public var toValue:F4Value;
		public var fromValue:F4Value;
		
		private var toIndex:int;
		private var fromIndex:int;
		//------------------------------------------	
		
		[Bindable]
		public var selectedListSM:ArrayCollection = new ArrayCollection();
		
		[Bindable]
		public var selectedListSMVO:ArrayCollection = new ArrayCollection();
		
		[Bindable]
		public var sourceDataList:XMLList = new XMLList();
		
		[Bindable]
		private var _f4RangeValues:XMLList = new XMLList();
		
		
		

		//[Inject("queryModel.f4Data", bind="true")]
		public function get f4RangeValues():XMLList
		{
			return _f4RangeValues;
		}

		public function set f4RangeValues(value:XMLList):void
		{
			_f4RangeValues = value;
			
			//_f4RangeValues.refresh();
		}

		[Inject("queryModel.f4Xml", bind="true")]
		public function get f4Values():XMLList
		{
			return _f4Values;
		}
		
		public function set f4Values(value:XMLList):void
		{
			_f4Values = value;
			sourceDataList = value;
			
		//	userListViewAdded(view);
				
			//showSelectedValues();
		}
		
		
		[ViewAdded]
		public function userListViewAdded( view : F4_CC_SELEC_POPUP ) : void
		{
			trace('Abbas: F4_CC_SELEC_POPUP');
			if(this.view == null)
			{
				selectionMeta = view.selectionMeta;
				this.view = view;
				this.view.addEventListener(FlexEvent.CREATION_COMPLETE, loadF4Query);
				this.view.addEventListener(FlexEvent.INITIALIZE, initilize);
				trace('SelectionReport: F4_CC_SELEC_POPUP');
			}
		}
		
		private function initilize(event:FlexEvent):void
		{
			trace("F4_CC_SELEC_POPUP: Initilized");
		}
		private function loadF4Query(event:FlexEvent):void
		{
			trace("F4_CC_SELEC_POPUP: CREATION_COMPLETE");
			var myEvent:LoadQueryEvent = new LoadQueryEvent(LoadQueryEvent.LOAD_XML_F4_EVENT);
			
			myEvent.parm = new Object();
			myEvent.parm['iobjnm'] = selectionMeta.variableMeta.iobjnm;
			dispatcher.dispatchEvent(myEvent);
			
			//	dispatcher.addEventListener(
		}
		public function close():void
		{	var parentContainer:BASE_POPUP_SELECTION = selectionMeta.parentContainer as BASE_POPUP_SELECTION;	
			returnPopupSelection();
			
			//f4Values = new ArrayCollection();
			//f4RangeValues = new ArrayCollection();
			PopUpManager.removePopUp(this.view);
		}
		
		private function returnPopupSelection():SelectionVo
		{
			var parentContainer:BASE_POPUP_SELECTION = selectionMeta.parentContainer as BASE_POPUP_SELECTION;	
			 
			 if(selectedListSM.length > 0)
			 {
				 parentContainer.currentSelection.value.singleMultipleSelection.removeAll();
				 
				 for each( var selectedValue:F4Value in selectedListSM)
				 {
					 parentContainer.currentSelection.value.singleMultipleSelection.addAll(selectedListSMVO);
				 }
			 }
			 
			 if(toValue != null && fromValue && null)
			 {
				 parentContainer.currentSelection.value.rangeVo.from = fromValue.key;
				 parentContainer.currentSelection.value.rangeVo.to = toValue.key;
			 }
			 
			 
			 parentContainer.currentSelection.value = parentContainer.currentSelection.value;
			 
			return parentContainer.currentSelection;
		}
		
		
		
		//Range Selection
		public function moveRangeFromRight():void
		{
			if(fromValue != null)
				f4RangeValues.addItemAt(fromValue,fromIndex);
			
			fromValue = null;
		}
		
		public function moveRangeFromLeft():void
		{
			if(view.sourceRange_dg2.selectedItems.length > 0)
			{
			    fromValue =	view.sourceRange_dg2.selectedItem as F4Value; 
				fromIndex = f4RangeValues.getItemIndex(fromValue);
				f4RangeValues.removeItemAt(fromIndex);
			}
		}
		
		public function moveRangeToRight():void
		{
			if(toValue != null)
				f4RangeValues.addItemAt(toValue,toIndex);
			
			toValue = null;
		}
		
		public function moveRangeToLeft():void
		{
			if(view.sourceRange_dg2.selectedItems.length > 0)
			{
				toValue =	view.sourceRange_dg2.selectedItem  as F4Value; 
				toIndex = f4RangeValues.getItemIndex(toValue);
				f4RangeValues.removeItemAt(toIndex);
			}
		}
		
		// Single/Multiple Selection
		public function moveSingleLeft(sourceGrid:Array):void
		{
			if(sourceGrid != null)
			{
				for(var x:int=0; x < sourceGrid.length; x++)
				{
					var item:F4Value = sourceGrid[x];
					selectedListSM.addItem(item);
					selectedListSMVO.addItem(new SingleMultipleSelectionVo(item.text, item.key,true));
					f4Values.removeItemAt(f4Values.getItemIndex(sourceGrid[x]));
					
				}
				f4Values.refresh(); 
				selectedListSMVO.refresh();
				selectedListSM.refresh();
				showIncludeExcludeToggleButtonBar();
			}
		}
		
		
		public function moveSingleRight(selectedList:ArrayCollection, items:Array):void
		{
			var seletcted:Array = items.sort();
			for(var x:int= selectedList.length; x >= 0; x--)
			{
				if(items.indexOf(x) != -1)
				{
					var selectedItem:F4Value = selectedList.removeItemAt(x) as F4Value;
					f4Values.addItemAt(selectedItem,f4Values.length);
					SelectionUtil.removeObjectByAttribute(selectedListSMVO,"value",selectedItem.key);
					
				} 
			}
			selectedListSM.refresh();
			selectedListSMVO.refresh();
			f4Values.refresh(); 
			showIncludeExcludeToggleButtonBar();
		}
		
		public function moveAllLeft():void
		{
			if(f4Values != null)
			{
				for(var x:int=0; x < f4Values.length; x++)
				{
					selectedListSM.addItem(f4Values[x]);
				}
				
				f4Values.removeAll();
				
				selectedListSM.refresh();
				f4Values.refresh(); 
				
				showIncludeExcludeToggleButtonBar();
			}
		}
		
		public function moveAllRight():void
		{
			if(selectedListSM != null)
			{
				for(var x:int=0; x < selectedListSM.length; x++)
				{
					f4Values.addItem(selectedListSM[x]);
				}

				selectedListSM.removeAll();
				
				selectedListSM.refresh();
				f4Values.refresh(); 
				
				showIncludeExcludeToggleButtonBar();
			}
		}
		
		// UI Functions------------------------
		
		private function showIncludeExcludeToggleButtonBar():Boolean
		{
			if((this.view.selected_dg1.dataProvider as ArrayCollection).length > 0)
			{
				this.view.includeExcludeToggleButtonBar.visible = true;
				return true;
			}
			else
			{
				this.view.includeExcludeToggleButtonBar.visible = false;
				return false;
			}
		}
		
		private function checkIncludedExcluded():Boolean
		{
			if((this.view.selected_dg1.dataProvider as ArrayCollection).length == 0)
			{
				return includeExcluded_ToggleButtonBar_Selected;
			}
			else
			{
				return (((this.view.selected_dg1.dataProvider as ArrayCollection)[0]) as SingleMultipleSelectionVo).included;
			}
		}
		
		public function toggleIncludedExcludedIcon(control:mx.controls.ToggleButtonBar):void
		{
			var item:Object;
			for( var x:int=0; x < control.dataProvider.length; x++)
			{
				item = control.dataProvider[x];
				if(item.icon == view.GreenBall || item.icon == view.RedBall)
					item.icon = view.GreyBall;
				else
					item.label == "Included" ? view.GreenBall : view.RedBall;
			}
		}
		
		public function showSelectedValues():void
		{
			var selectionVObject:SelectionVo;
			var parentContainer:BASE_POPUP_SELECTION;
			if(selectionMeta != null)
				parentContainer = selectionMeta.parentContainer as BASE_POPUP_SELECTION;
			
			if(selectionMeta != null && parentContainer.currentSelection == null)
			{
				 selectionVObject  = SelectionUtil.formatVariablesScreenMetadata(selectionMeta.variableMeta);
				 parentContainer.currentSelection =  selectionVObject;
				 //selectSelectionVO(selectionVObject);
			}
			
			else
			{
				//selectSelectionVO(parentContainer.currentSelection);
			}
		}
		
		private function selectSelectionVO(selectionVObject:SelectionVo):void
		{
			if(selectionVObject.value != null)
			{
				if(selectionVObject.value.rangeVo != null)
				{
					
					var defalutFrom:F4Value =	SelectionUtil.findXmlObjectByAttribute(f4RangeValues,"key",selectionVObject.value.rangeVo.from) as F4Value;
					if(defalutFrom != null)
					{
						view.sourceRange_dg2.selectedItems = new Array(defalutFrom);
						moveRangeFromLeft();
					}
						
					else
					{
						fromValue = new F4Value(selectionVObject.value.rangeVo.from,"");
					}
					
					var defalutTo:F4Value =	SelectionUtil.findXmlObjectByAttribute(f4RangeValues,"key",selectionVObject.value.rangeVo.to) as F4Value;
					if(defalutFrom != null)
					{
						view.sourceRange_dg2.selectedItems = new Array(defalutTo);
						moveRangeToLeft();
					}
						
					else
					{
						toValue = new F4Value(selectionVObject.value.rangeVo.to,"");
					}
					
				}
				
				if(selectionVObject.value.singleMultipleSelection != null && selectionVObject.value.singleMultipleSelection.length > 0)
				{
					for each(var SMS:SingleMultipleSelectionVo in selectionVObject.value.singleMultipleSelection)
					{
						var defalutValue:F4Value =	SelectionUtil.findXmlObjectByAttribute(f4Values,"key", SMS.VALUE) as F4Value;
						if(defalutValue != null)
						{
							//view.sourceSingleMultiple_dg1.selectedItem = ;
							view.sourceSingleMultiple_dg1.selectedItems = new Array(defalutValue); 
							moveSingleLeft(view.sourceSingleMultiple_dg1.selectedItems);
						}
						else
						{
							
							selectedListSM.addItem(new F4Value(SMS.VALUE,SMS.NAME));
							
							f4Values.refresh(); 
							selectedListSM.refresh();
							showIncludeExcludeToggleButtonBar();
						}
					}
					
				}
				
			}
		}
		
	}
}